import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:text_world/global.dart';
import 'package:text_world/main_manager.dart';
import 'package:text_world/pages/dots.dart';
import 'package:text_world/pages/yiju_card.dart';
import 'package:text_world/pages/yiwen_card.dart';

class LikePage extends StatefulWidget {
  LikePage({Key key}) : super(key: key);
  @override
  _LikePageState createState() => _LikePageState();
}

class _LikePageState extends State<LikePage> {
  @override
  void initState() {
    super.initState();
  }

  PageController _pageController = PageController(
    initialPage: 0,
    viewportFraction: 1,
    keepPage: true,
  );

  int _page = 0;

  List<Widget> getChildrenList(list) {
    List<Widget> _children = [];
    List.generate(list.length, (index) {
      String dataStr = json.encode(list[index]);
      final item = json.decode(dataStr);
      if (item['card_type'] == 'hitokoto') {
        _children.add(Center(child: YijuCard(hitokoto: item)));
      } else {
        _children.add(YiWenCard(yiwen: item));
      }
    });
    return _children;
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 50,
        elevation: 0,
        title: Text(
          "喜欢",
          style: TextStyle(
            color: Global.fontColor,
            fontSize: 17,
            fontFamily: 'fangzhengkai',
            fontWeight: FontWeight.w600,
          ),
        ),
        leading: _mainManager.likeList.length == 0
            ? Container()
            : IconButton(
                icon: Icon(
                  _mainManager.isLike(_mainManager.likeList[_page])
                      ? Icons.favorite
                      : Icons.favorite_border,
                  color: _mainManager.isLike(_mainManager.likeList[_page])
                      ? Global.likeColor
                      : Global.primaryColor,
                ),
                tooltip: '收藏',
                onPressed: () {
                  if (_page > _mainManager.likeList.length - 1) {
                    setState(() => {_page = _mainManager.likeList.length - 1});
                  }
                  if (_mainManager.likeList.length == 1) {
                    setState(() => {_page = 0});
                  }
                  _pageController.animateToPage(0,
                      duration: Duration(milliseconds: 1), curve: Curves.ease);
                  _mainManager.like(_mainManager.likeList[_page]);
                },
              ),
      ),
      body: _mainManager.likeList.length == 0
          ? Center(
              child: Text(
              "收藏的文字会在这里展示哦！",
              style: TextStyle(color: Global.fontSecondColor),
            ))
          : Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              color: Global.backgroundColor,
              child: Stack(
                children: [
                  Positioned(
                    top: 0.0,
                    bottom: 0.0,
                    left: 0.0,
                    right: 0.0,
                    child: PageView(
                      controller: _pageController,
                      scrollDirection: Axis.horizontal,
                      physics: BouncingScrollPhysics(),
                      pageSnapping: true,
                      children: getChildrenList(_mainManager.likeList),
                      onPageChanged: (int page) {
                        print(page);
                        setState(() => {_page = page});
                      },
                    ),
                  ),
                  Positioned(
                    bottom: 0.0,
                    left: 0.0,
                    right: 0.0,
                    child: Container(
                      padding: const EdgeInsets.all(20.0),
                      child: Center(
                        child: SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: DotsIndicator(
                            color: Global.primaryColor,
                            controller: _pageController,
                            itemCount: _mainManager.likeList.length,
                            onPageSelected: (int page) {
                              setState(() => {_page = page});
                              _pageController.animateToPage(
                                page,
                                duration: Duration(milliseconds: 300),
                                curve: Curves.ease,
                              );
                            },
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
    );
  }
}
